<?php
require_once ('conn.php');
$msg='n';
$buId=$_POST['buId'];//补货网点ID

$ci_contact = mysql_fetch_row(mysql_query("select number,bhstaffid from ci_contact where id=$buId "));//获得网点编号

$times=time();//初始化一个时间
$billNo ="XS".date('YmdHis',$times).rand(1000,9999);//生成唯一订单号
$num = mysql_fetch_row(mysql_query("select SUM(num) from ci_wx_order_goods where customerNumber ='".$ci_contact[0]."' and status=0  and payStatus = 1"));//购货总数量
$result2=mysql_query("select num,goodsPrice from ci_wx_order_goods where customerNumber ='".$ci_contact[0]."' and status=0  and payStatus = 1");//计算出总价格

$totalAmount=0;
while($n=mysql_fetch_array($result2)) 
{
  $totalAmount=$totalAmount+$n[0]*$n[1];
}
$amount=$totalAmount;//折扣后金额
$rpAmount=$totalAmount;//本次付款
$billDate=date('Y-m-d',$times);//日期
$description="微信小程序补货单";//备注
$arrears=$totalAmount;//本次欠款
$totalQty=$num[0];//总数量
$billType="SALE";
$modifyTime=date('Y-m-d H:i:s',$times);//更新时间
$hxStateCode=1;//0未付款  1部分付款  2全部付款
$transTypeName="补货";
$salesId=$_SESSION['userid'];
$sql="INSERT INTO ci_invoice(
					buId,
					billNo,
					transType,
					totalAmount,
					amount,
					billDate,
					description,
					arrears,
					

					totalQty,
                    salesId,



					billType,
					modifyTime,
					hxStateCode,
					transTypeName,
                    bhstaffid
				)
					VALUES (
					$buId,
					'$billNo',
					150601,
					$totalAmount,
					$amount,
					'$billDate',
					'$description',
					$arrears,


					$totalQty,
                    $salesId,


					'$billType',
					'$modifyTime',
					$hxStateCode,
					'$transTypeName',
                    $ci_contact[1]
					)";
                
				if(mysql_query($sql)){//成功插入主表才做下一步
		        $msg='y';
				$result=mysql_query("select * from ci_wx_order_goods where customerNumber ='".$ci_contact[0]."' and status=0  and payStatus = 1"); //每一个商品插入一行数据
				$i=0;
				$Totalnum = 0;
                $Totalprice = 0;
				while($row=mysql_fetch_array($result)) 
				{
				$ci_invoice = mysql_fetch_row(mysql_query("select id from ci_invoice where billNo='$billNo' "));
				$iid = $ci_invoice[0];//关联ID
				$num = mysql_fetch_row(mysql_query("select LEAST(sum(num),t3.qty) as bhNum, t1.customerNumber contactNumber, t1.goodsNumber as goodsNumber, t2.locationId as locationId, sum(num) as saleCount ,t3.qty as qty 
from ci_wx_order_goods as t1 INNER JOIN ci_contact as t2 on t1.customerNumber = t2.number
inner join ci_v_inventory as t3 on t3.locationId = t2.locationId
where t1.customerNumber='".$ci_contact[0]."' and t1.goodsNumber ='".$row['goodsNumber']."' and t3.number='".$row['goodsNumber']."' and t1.status =0 and t1.payStatus =1 group by t1.customerNumber,t1.goodsNumber
"));
                $locationId = $num[3];
                $goodsPrice = mysql_fetch_row(mysql_query("select goodsPrice from ci_wx_order_goods where goodsNumber ='".$row['goodsNumber']."'
                and customerNumber = '".$ci_contact[0]."' and status=0  and payStatus = 1"));
				$amount=$num[0]*$goodsPrice[0];//商品总金额
                $Totalprice = $Totalprice+$amount;//主表总价格
                $Totalnum = $Totalnum + $goodsPrice[0];//主表总数量
                
				$ci_goods = mysql_fetch_row(mysql_query("select id from ci_goods where number ='".$row['goodsNumber']."' "));
				$invId=$ci_goods[0];//商品ID
				$price=$goodsPrice[0];//商品单价
				$qty="-".$num[0];//商品数量
					
					
					$sql2="INSERT INTO ci_invoice_info(
					iid,
					buId,
					billNo,
					transType,
					amount,
					billDate,
					description,
					invId,
					price,
					
					qty,
					locationId,
					unitId,
					skuId,
					entryId,
					transTypeName,

					billType

				)
					VALUES (
					$iid,
					$buId,
					'$billNo',
					150601,
					$amount,
					'$billDate',
					'$description',
					$invId,
					$price,
					
					$qty,
					$locationId,
					-1,
					-1,
					1,
					

					'$transTypeName',

					'$billType'

					)";
                    mysql_query("UPDATE ci_wx_order_goods SET status = 2 WHERE  goodsNumber ='".$row['goodsNumber']."' and customerNumber ='".$ci_contact[0]."'  and payStatus = 1"); 
                    if(empty($num[0])||$num[0]<=0){
                        
                        continue;
                    }
                   
					mysql_query($sql2);
					$i++;
		}
         mysql_query("UPDATE ci_invoice SET totalAmount = $Totalprice, amount = $Totalprice,arrears = $Totalprice,totalQty = $Totalnum  where billNo='$billNo'");
	}
echo $msg;die;
?>